#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAX_SIZE 100 

// 定义栈的结构体
typedef struct {
    int data[MAX_SIZE]; 
    int top;    
} Stack;

void initStack(Stack *stack) {
    stack->top = -1; 
}

int isEmpty(Stack *stack) {
    return stack->top == -1;
}

int isFull(Stack *stack) {
    return stack->top == MAX_SIZE - 1;
}

void push(Stack *stack, int value) {
    if (isFull(stack)) {
        printf("栈满了 %d\n", value);
        return;
    }
    stack->data[++stack->top] = value;
    printf("把  %d 进栈\n", value);
}

// 出栈操作
int pop(Stack *stack) {
    if (isEmpty(stack)) {
        printf("栈空\n");
        exit(EXIT_FAILURE);
    }
    return stack->data[stack->top--];
}
int top(Stack *stack) {
    if (isEmpty(stack)) {
        printf("栈空\n");
        exit(EXIT_FAILURE);
    }
    return stack->data[stack->top];
}

int main() {
    SetConsoleOutputCP(65001);
    
    Stack stack;

    
    initStack(&stack);

    
    push(&stack, 10);
    push(&stack, 20);
    push(&stack, 30);

    
    printf("栈顶元素: %d\n", top(&stack));
    printf("出栈元素: %d\n", pop(&stack));

    
    printf("栈顶元素: %d\n", top(&stack));

    return 0;
}
